local gallop = require "QGMainWindow"
local nl = require"luaNumeric"
local plot = require "Q2DViewerEx"
local DataPost = require "DataPost"
require "unity"



-----------------------------------------------------------------------------
--初始化参数
local FarfieldsPost = DataPost:new();
-----------------------------------------------------------------------------



function FarfieldsPost:read_from_file(path)				 
	print(path);
	self.data_list = {}
	self.data = file2table( path,"\t")	
	self.str = file2StrTable( path,"\t")	
	--print( self.str )
	local nheader = self.nheader;
	self.nangle = #self.str - nheader - 1 ;
	self.nfields = #self.str[nheader]-2;
	self.labels = self.str[nheader];
	
	number = "[ ]*[+-]?%d+[.]?%d*[Ee+-]*%d*";
	fields_type = '(.*)[%[(]';
	self.index = { theta = {labels={}, lable={}}, 
			     phi = {labels={}, lable={}}, 
			     freq = {labels={}, lable={}}, 
			     fields_types = {labels={}, lable={}}, };
	self.keys = {"theta","phi","freq","fields_types"};
	
	for jj=1,self.nfields do
		local label = self.labels[2+jj];
		local freq =  string.match(label, number)
		local fts = string.match(label, fields_type)
		table.insert( self.index["freq"].labels, freq); 
		table.insert( self.index["fields_types"].labels, fts); 
			
		for ii=1,self.nangle do
			local theta = self.data[ii+nheader+1][1];
			local phi = self.data[ii+nheader+1][2];
			local key = self:get_key({theta,phi,freq,fts})
			self.data_list[key] = self.data[ii+nheader+1][2+jj];
			table.insert( self.index["theta"].labels, theta); 
			table.insert( self.index["phi"].labels, phi); 
		end
	end
	
	assert( self.nangle>0, "import theta empty" )
	assert( self.nfields>0, "import theta empty" )
	
	print( "Import fields succesfully, angle size:".. self.nangle .. ' fields size:' .. self.nfields ..' data size:' .. #self.data_list );
	
	self:compress_idx();
	
	table.sort(self.index.theta.label,function (a,b) return tonumber(a)<tonumber(b) end )
	table.sort(self.index.phi.label,function (a,b) return tonumber(a)<tonumber(b) end )
	table.sort(self.index.freq.label,function (a,b) return tonumber(a)<tonumber(b) end )

end	



function FarfieldsPost:read_from_model()				 

	local  farboxs = Geometry.farbox_list(); 
     
	for k,v in pairs(farboxs) do
		res = v:results();	  
		self.theta = res.theta;
		self.phi = res.phi;
		self.freq = res.freq;
		self.etheta_data = res.etheta;
		self.ephi_data =res.ephi;	  
	end
	
	self.index = { theta = {labels={}, lable={}}, 
			     phi = {labels={}, lable={}}, 
			     freq = {labels={}, lable={}}, 
			     fields_types = {labels={}, lable={}}, };
	self.keys = {"theta","phi","freq","fields_types"};
	self.data_list = {}
	self.nangle = #self.theta;
	self.nfreq = #self.freq;
	
	table.insert( self.index["fields_types"].labels, "RCS-θ"); 
	table.insert( self.index["fields_types"].labels, "RCS-φ"); 
	table.insert( self.index["fields_types"].labels, "RCS-total"); 

	for kk,vv in pairs (self.freq) do
		local freq = tostring(vv);
		table.insert( self.index["freq"].labels, tostring(vv)); 
		  
		for ii=1,self.nangle do
			local theta = tostring(self.theta[ii]);
			local phi = tostring(self.phi[ii]);
			table.insert( self.index["theta"].labels, theta); 
			table.insert( self.index["phi"].labels, phi); 
			
			local fts = "RCS-θ";
			local key = self:get_key({theta,phi,freq,fts})
			self.data_list[key] = nl.abs( self.etheta_data[kk][ii] );
			
			local fts = "RCS-φ";
			local key = self:get_key({theta,phi,freq,fts})
			self.data_list[key] = nl.abs( self.ephi_data[kk][ii] );
			
			local fts = "Etotal";
			local key = self:get_key({theta,phi,freq,fts})
			self.data_list[key] = nl.abs( self.ephi_data[kk][ii] )+nl.abs( self.etheta_data[kk][ii] );

		end
	end
	
	self:compress_idx();
	
	table.sort(self.index.theta.label,function (a,b) return tonumber(a)<tonumber(b) end )
	table.sort(self.index.phi.label,function (a,b) return tonumber(a)<tonumber(b) end )
	table.sort(self.index.freq.label,function (a,b) return tonumber(a)<tonumber(b) end )
		
end

function FarfieldsPost:plot_farfield ()
	if self.view ~= nil then
		 local new_idx = {}
		 for k,v in pairs(self.index) do
			 new_idx[k] = clone(v.label);
		 end
		 new_idx.theta = nil;	
		 new_idx.fields_types = {"Etotal"};	
		 self:plot_curves("theta", new_idx)
	end
end

return FarfieldsPost;


